package cn.kgc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.kgc.entity.ResumeWorkExp;
import cn.kgc.form.ResumeWorkExpAddOrUpdateForm;
import cn.kgc.mapper.ResumeWorkExpMapper;
import cn.kgc.service.ResumeWorkExpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author YC
 */
@Slf4j
@Service("resumeWorkExpService")
public class ResumeWorkExpServiceImpl extends ServiceImpl<ResumeWorkExpMapper, ResumeWorkExp> implements ResumeWorkExpService {

    @Override
    public List<ResumeWorkExp> queryResumeWorkExpByResumeId(String resumeId, String userId) {
        return baseMapper.selectList(
                new LambdaQueryWrapper<ResumeWorkExp>()
                        .eq(ResumeWorkExp::getResumeId, resumeId)
                        .eq(ResumeWorkExp::getUserId, userId)
                        .orderByAsc(ResumeWorkExp::getEndDate)
        );
    }

    @Override
    public boolean addOrModifyResumeWorkExp(ResumeWorkExpAddOrUpdateForm form, String userId) {
        if (form.getId().isEmpty()) {
            // 没传入ID为新增操作
            ResumeWorkExp resumeWorkExp = BeanUtil.copyProperties(form, ResumeWorkExp.class);
            resumeWorkExp.setUserId(userId);
            return baseMapper.insert(resumeWorkExp) > 0;
        } else {
            // 传入ID为修改操作
            return this.update(
                    new LambdaUpdateWrapper<ResumeWorkExp>()
                            .eq(ResumeWorkExp::getId, form.getId())
                            .eq(ResumeWorkExp::getUserId, userId)
                            .eq(ResumeWorkExp::getResumeId, form.getResumeId())
                            .set(ResumeWorkExp::getCompanyName, form.getCompanyName())
                            .set(ResumeWorkExp::getIndustry, form.getIndustry())
                            .set(ResumeWorkExp::getBeginDate, form.getBeginDate())
                            .set(ResumeWorkExp::getEndDate, form.getEndDate())
                            .set(ResumeWorkExp::getPosition, form.getPosition())
                            .set(ResumeWorkExp::getDepartment, form.getDepartment())
                            .set(ResumeWorkExp::getContent, form.getContent())
                            .set(ResumeWorkExp::getContentHtml, form.getContentHtml())
            );
        }
    }

    @Override
    public boolean deleteResumeWorkExp(String id, String resumeId, String userId) {
        return baseMapper.delete(
                new LambdaQueryWrapper<ResumeWorkExp>()
                        .eq(ResumeWorkExp::getId, id)
                        .eq(ResumeWorkExp::getResumeId, resumeId)
                        .eq(ResumeWorkExp::getUserId, userId)
        ) > 0;
    }

}
